package com.zxp.server.controller;


import com.zxp.server.config.Result;
import com.zxp.server.dto.UserLogin;
import com.zxp.server.feign.FeignServer;
import com.zxp.server.service.IUserService;
import com.zxp.server.util.File.FileService;
import com.zxp.server.util.MD5Util;
import com.zxp.server.util.token.JwtUtil;
import com.zxp.server.util.token.annotation.CheckToken;
import com.zxp.server.vo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 朱小皮
 * @since 2022-05-04
 */
@Api(value = "用户管理", tags = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService iUserService;

    @Autowired
    FileService fileService;

    @Autowired
    FeignServer feignServer;

    @ApiOperation(value = "用户注册")
    @PostMapping("/addUser")
    public Result addUser(@RequestBody User user){
        if (iUserService.userExists(user.getUsername())&&iUserService.addUser(user)==1){
            return Result.success("注册成功");
        }
        return Result.error("用户名已存在，请重试");
    }


    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public Result login(@RequestBody UserLogin userLogin, HttpServletRequest request){
        return  iUserService.login(userLogin,request);
    }

    @CheckToken
    @ApiOperation(value = "验证用户是否登录")
    @GetMapping("/checkUser")
    public Result checkUser(){
        return  Result.success("已登录");
    }

    @ApiOperation(value = "获取当前登录用户信息")
    @PostMapping("/userInfo")
    public Result userInfo(HttpServletRequest request){
        String token = request.getHeader("authorization");
        String userId;
        try {
            userId = JwtUtil.parseJWT(token).get("id").toString();
        } catch (Exception exception) {
            exception.printStackTrace();
            return Result.error("用户信息验证异常");
        }
        User user = iUserService.findUserById(userId);
        return Result.success("获取当前用户信息成功",user);
    }

    @ApiOperation(value = "修改用户头像")
    @PostMapping("/postUserFace/{id}")
    public Result postUserFace(@PathVariable String id,MultipartFile file){
        Result userFace = feignServer.PostFile(file);
        User user = iUserService.findUserById(id);
        user.setUserface(userFace.getData().toString());
        iUserService.updateUser(user);
        return Result.success("头像更新成功");
    }

    @ApiOperation(value = "修改用户信息")
    @PostMapping("/userEdit")
    public Result userEdit(@RequestBody User user,HttpServletRequest request){
        User userInfo = iUserService.findUserById(String.valueOf(user.getId()));
        userInfo.setEmail(user.getEmail());
        userInfo.setEmail(user.getNickname());
        userInfo.setPhone(user.getPhone());
        iUserService.updateUser(user);
        Result result = this.userInfo(request);
        return Result.success("修改成功",result.getData());
    }

    @ApiOperation(value = "修改用户密码")
    @PostMapping("/passwdEdit")
    public Result passwdEdit(@RequestBody HashMap<String,String> map){
        User user = iUserService.findUserById(map.get("id"));
        user.setPassword(MD5Util.getMD5(map.get("pwd1")));
        iUserService.updateUser(user);
        return Result.success("修改密码成功");
    }
}
